package com.example.design.beautiful.unit_test.performance_counter.v2;

import com.example.design.beautiful.unit_test.performance_counter.collector.MetricsCollector;
import com.example.design.beautiful.unit_test.performance_counter.collector.RequestInfo;
import com.example.design.beautiful.unit_test.performance_counter.strorage.MetricsStorage;
import com.example.design.beautiful.unit_test.performance_counter.strorage.RedisMetricsStorage;
import com.example.design.beautiful.unit_test.performance_counter.v2.reporter.ConsoleReporter;
import com.example.design.beautiful.unit_test.performance_counter.v2.reporter.EmailReporter;
import com.example.design.beautiful.unit_test.performance_counter.v2.view.ConsoleViewer;
import com.example.design.beautiful.unit_test.performance_counter.v2.view.EmailViewer;

public class PerfCounterTest {
  public static void main(String[] args) {
    MetricsStorage storage = new RedisMetricsStorage();
    Aggregator aggregator = new Aggregator();

    // 定时触发统计并将结果显示到终端
    ConsoleViewer consoleViewer = new ConsoleViewer();
    ConsoleReporter consoleReporter = new ConsoleReporter(storage, aggregator, consoleViewer);
    consoleReporter.startRepeatedReport(60, 60);

    // 定时触发统计并将结果输出到邮件
    EmailViewer emailViewer = new EmailViewer();
    emailViewer.addToAddress("wangzheng@xzg.com");
    EmailReporter emailReporter = new EmailReporter(storage, aggregator, emailViewer);
    emailReporter.startDailyReport();

    // 收集接口访问数据
    MetricsCollector collector = new MetricsCollector(storage);
    collector.recordRequest(new RequestInfo("register", 123, 10234));
    collector.recordRequest(new RequestInfo("register", 223, 11234));
    collector.recordRequest(new RequestInfo("register", 323, 12334));
    collector.recordRequest(new RequestInfo("login", 23, 12434));
    collector.recordRequest(new RequestInfo("login", 1223, 14234));

    try {
      Thread.sleep(100000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}